﻿@using Amba.ImagePowerTools.HtmlHelpers
@model Amba.ImagePowerTools.ViewModels.Multipicker.MediaFolderEditViewModel
@{
    Style.Require("jQueryUI_Orchard").AtHead();
    Script.Require("jQueryUI_Tabs").AtHead();

    SetMeta("X-UA-Compatible", "IE=edge,chrome=1");

    Style.Include("~/themes/theadmin/styles/site.css");
    Style.Include("~/themes/theadmin/styles/ie.css").UseCondition("lte IE 8").SetAttribute("media", "screen, projection");
    Style.Include("~/themes/theadmin/styles/ie6.css").UseCondition("lte IE 6").SetAttribute("media", "screen, projection");
    Style.Include("media-multipicker.css").AtFoot();

    var isSearch = !string.IsNullOrWhiteSpace(Model.SearchFilter);
}
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title>@T("Image Multipicker")</title>
        @Display.Metas()
        @Display.HeadScripts()
        @Display.HeadLinks()
        @Display.StyleSheetLinks()
    </head>
    <body id="multipicker">
        <div class="top-buttons">
        </div>
        <div class="picker-content-box">
            @if (Model.IsFolderNotExists)
            {
                <div>@T("Folder does not exists")</div>
            }
            else
            {
                <div class="breadcrumb-box">
                    <a href="@Model.GetPickerUrl("")">@T("Media Folder")</a>
                    @foreach (var breadcrumbItem in Model.BreadCrumbs)
                    {
                        <text>&gt;</text>
                        <a href="@Model.GetPickerUrl(@breadcrumbItem.MediaPath)">
                            @breadcrumbItem.FolderName
                        </a>
                    }
                </div>
                
                    using (Html.BeginForm("Search", "Multipicker", new { area = "Amba.ImagePowerTools" }))
                    {
                    <div class="search-fields">
                        <input type="text" name="search" id="filesearch-input" value="@(Model.SearchFilter)" /> 
                        <button id="search-button">@T("Search")</button>
                        <input type="hidden" value="@Model.MediaPath" name="mediaPath"/>
                        <input type="hidden" value="@Model.Scope" name="scope"/>
                    </div>
                    }                                                   
                <form>
                    <table class="file-browser" style="min-width: 340px;">
                        @foreach (var folder in Model.Folders)
                        {
                            <tr class="folder-row">
                                <td class="icon-col" style="width: 20px;">
                                    <span class="folder-icon"></span>
                                </td>
                                <td colspan="2">
                                    <a href="@Model.GetPickerUrl(folder.SitePath)">
                                        <b>@folder.Name</b>
                                    </a>
                                </td>
                            </tr>
                        }
                        @foreach (var file in Model.Files)
                        {
                            <tr>
                                <td style="width: 20px;">
                                    <input type="checkbox" class="file-picker-checkbox" value="@(file.SitePath)" name="selected"/>
                                </td>
                                <td style="width: 100px; padding:4px;">
                                    <a href="@Url.Content("~" + file.SitePath)" target="_blank">
                                        @if (file.IsImage)
                                        {
                                            <img src="@Url.Content("~/ipt/resize" + file.SitePath + "?width=100")" alt="" />
                                        }
                                        else
                                        {
                                            @Html.ResizedImage("/modules/Amba.ImagePowerTools/content/" + file.Extension + ".png", settings: "width=100")
                                        }
                                    </a>
                                </td>
                                <td>
                                    <div>
                                        @{
                                        var html = string.Empty;
                                        }
                                        @if (isSearch)
                                        {
                                            var path = string.Empty;
                                            foreach (var folder in file.SiteFolder.Split('/'))
                                            {
                                                if (string.IsNullOrWhiteSpace(folder))
                                                {
                                                    continue;
                                                }
                                                if (!string.IsNullOrWhiteSpace(path))
                                                {
                                                    path += "/";
                                                }
                                                path += folder;
                                                html += "<a href=\'" + Model.GetPickerUrl(path) + "'>" + folder + "</a>/";
                                            }
                                        }
                                        @Html.Raw(html)<a href="@file.SitePath" target="_blank">@file.FileName</a>
                                    </div>
                                    <div>
                                        @if (file.Width > 0 && file.Height > 0)
                                        {
                                            <text>@file.Width x @file.Height</text> 
                                        }
                                        else
                                        {
                                            <text>@file.Extension @T("file")</text>
                                        }
                                    </div>
                                    @if (file.Lon.HasValue && file.Lat.HasValue)
                                    {
                                        <div>
                                            <img src="@Url.Content("~/modules/Amba.ImagePowerTools/content/pointer.png")" height="12px" /> @string.Format("{0:0.000000}, {0:0.000000}", file.Lon, file.Lat)
                                        </div>
                                    }
                                    
                                </td>
                            </tr>        
                        }
                    </table>
                </form>
            }
        </div>
                
        @Display.FootScripts()
        <script type="text/javascript">
            $().ready(function () {
                var client = window.opener.@(Model.Scope);
                var selected = client.selected ? client.selected() : [];
                $(".file-picker-checkbox").each(function(i, el){
                    var $el = $(el);
                    var val = $el.val();
                    if ($.inArray(val, selected) >= 0)
                    {
                        $el.attr("checked", "checked");
                    }
                });

                $(".file-picker-checkbox").change(function() {
                    var isCheckd = $(this).is(":checked");
                    var fileName = $(this).val();
                    if (isCheckd) {
                        client.add(fileName);
                    } else {
                        if (client.remove)
                            client.remove(fileName);
                    }
                    if (!client.isMultiselect || !client.isMultiselect()) {
                        window.close();
                    }
                });
            });
        </script>

    </body>
</html>